numericCHR<- function(CHR, prefix="chr") {
  # Set autosomal chromosomes to their number
  # X - 98
  # Y - 99
  # XY - 100
  # MT, M - 101
  # Variable regions - 102
  CHR<-sub(paste0("^",prefix),"",as.character(CHR))
  CHR[grep("_",CHR)]<-"102"
  CHR[CHR=="X"]<-"98"
  CHR[CHR=="Y"]<-"99"
  CHR[CHR=="XY"]<-"100"
  CHR[CHR %in% c("M","MT")]<-"101"
  as.numeric(CHR)
}
#
characterCHR<- function(CHR,prefix="") {
  CHR<-as.character(CHR)
  CHR[CHR=="98"]<-"X"
  CHR[CHR=="99"]<-"Y"
  CHR[CHR=="100"]<-"XY"
  CHR[CHR=="101"]<-"MT"
  CHR[CHR=="102"]<-"-" 
  paste(prefix,CHR,sep="")
}


grid.semicircle <- function(oritention = oritention,pos = pos,centromere=centromere,order = order,nrows = nrows,...)
{
  
  #左1半圆
  if (oritention =="left" & order ==1){
    
    mydot<- seq(pi,2*pi,length=100)
    aRadius = pos[1,2]-pos[1,1]
    bRadius = 0.05
    
    xx = aRadius*sin(mydot) + pos[1,2]
    yy = bRadius*cos(mydot) + 0.5
    
    return(polygonGrob(xx, yy ,gp=gpar(fill = "#606060",col="#BFBFBF"),default.units = "native"))
  }
  
  #右1半圆
  else if (oritention =="right" & order ==1)
  {
    
    mydot<- seq(0,pi,length=100)
    aRadius = pos[centromere,2]-pos[centromere,1]
    bRadius = 0.05
    
    xx = aRadius*sin(mydot) + pos[centromere-1,2]
    yy = bRadius*cos(mydot) + 0.5
    
    return(polygonGrob(xx, yy, gp=gpar(fill = "#606060",col="#BFBFBF"),default.units = "native"))
  }
  
  #左2半圆
  else if (oritention =="left" & order ==2){
    
    mydot<- seq(pi,2*pi,length=100)
    aRadius = pos[centromere+1,2]-pos[centromere+1,1]
    bRadius = 0.05
    
    xx = aRadius*sin(mydot) + pos[centromere+1,2]
    yy = bRadius*cos(mydot) + 0.5
    
    return(polygonGrob(xx, yy ,gp=gpar(fill = "#606060",col="#BFBFBF"),default.units = "native"))
  }
  
  #右2半圆
  else if (oritention =="right" & order ==2)
  {
    
    mydot<- seq(0,pi,length=100)
    aRadius = pos[nrows,2]-pos[nrows,1]
    bRadius = 0.05
    
    xx = aRadius*sin(mydot) + pos[nrows-1,2]
    yy = bRadius*cos(mydot) + 0.5
    
    return(polygonGrob(xx, yy, gp=gpar(fill = "#606060",col="#BFBFBF"),default.units = "native"))
  }  
  
}


oneChromo<- function(mydata = mydata,chr=chr,angle = angle,...){
  
  chromdata <- mydata[mydata$chr==chr,]
  lc <- nchar(chromdata$band)
  sel <- !(substr(chromdata$band, lc, lc) %in% letters)
  chromdata <- chromdata[sel, ]
  rm(lc,sel)
  
  type.b<-match(chromdata$stain,
                c("acen","gneg", "gpos", "gvar", "stalk","gpos25","gpos50","gpos75","gpos100","gpos33", "gpos66"))
  bandpos<-chromdata[,c("segstart","segend")]
  
  n<-nrow(chromdata)
  centromere<-which(chromdata$arm[-n]!=chromdata$arm[-1])
  #需要绘制矩形的位置
  if (length(centromere==1)) {
    idx<-c(2:(centromere-1), (centromere+2):(n-1))
  } else {
    idx<-c(2:(n-1))
  }
  
  
  rg <- rectGrob(x = bandpos[idx,1], y = 0.45, width = bandpos[idx,2] -bandpos[idx,1], 
                 height = 0.1, just = c("left","bottom"), default.units = "native",gp=gpar(fill=mycol))
  
  # #半圆
  sg1 <- grid.semicircle(oritention = "left",order = 1,pos = bandpos,...)
  sg2 <- grid.semicircle(oritention = "right",order = 1,pos = bandpos,centromere = centromere,...)
  sg3 <- grid.semicircle(oritention = "left",order = 2,pos = bandpos,centromere = centromere,...)
  sg4 <- grid.semicircle(oritention = "right",order = 2,pos = bandpos,nrows=n,...)
  # #圆
  # #radius = ((bandpos[centromere,2]-bandpos[centromere,1]) + (bandpos[centromere+1,2]-bandpos[centromere+1,1]))/4
  cg <- grid.circle(bandpos[centromere,2], 0.5, unit(0.1*0.2,"npc"),, default.units="native", gp = gpar(col="black", fill="white", lwd=2))
  
  oneChromoGrob <- gTree(
    
    children = gList(
      rg,
      sg1,
      sg2,
      sg3,
      sg4,
      cg),
    vp = viewport(xscale = c(bandpos[1,1] , bandpos[n,2] ), yscale = c(0, 1), clip = "on",angle= angle)
    
  )
  return(oneChromoGrob)
}


plot_chromosome <- function(file = file,chr = chr,col = col,angle = 0,...)
{
  
  #输入文件
  ucscdata <- read.table(file,sep="",header = F)
  #处理特殊染色体
  chrombands<- data.frame(chr=characterCHR(numericCHR(ucscdata[,1])),segstart=ucscdata[,2],segend=ucscdata[,3],stain=ucscdata[,5],band=substring(ucscdata[,4],2),arm=substring(ucscdata[,4],1,1), stringsAsFactors=FALSE)
  
  #绘制全部染色体
  if (missing(chr)){
    #颜色设置
    
    #新画布
    grid.newpage()
    
  }
  #绘制指定的任意一条染色体
  else{
    
    grid.newpage()
    pushViewport(viewport(0.5,0.5,0.9,0.9))
    oneChromoGrob <- oneChromo(chrombands,chr,angle,...)
    grid.draw(oneChromoGrob)
    popViewport()
    
  }
}


mycol = brewer.pal(12,"Paired")
plot_chromosome(file="E:/workdir/jobs/R-package-source/quantsmooth/cytoband.txt",
                chr=22,col=mycol,angle = 0)

